package com.atxiaodei.kabudashejimoshi.单例模式.lazy;


/**
 * 优点:性能高了，线程安全了
 * 缺点：可读性难度加大，不够优雅
 */
// 双重检查懒汉式，防止多线程破坏单例
public class LazyDoubleCheckSingleton {

    private volatile static LazyDoubleCheckSingleton instance;

    private LazyDoubleCheckSingleton(){}

    public static LazyDoubleCheckSingleton getInstance(){
        //检查是否要阻塞
        if (instance == null) {

            synchronized (LazyDoubleCheckSingleton.class) {

                //检查是否要重新创建实例
                if (instance == null) {

                    instance = new LazyDoubleCheckSingleton();
                    //指令重排序的问题
                }

            }
        }
        return instance;
    }
}
